<template>
  <div id="index">
    <dyd-table :data="tableData">
      <el-table-column
        prop="ids"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        label="考试标题">
      </el-table-column>
      <el-table-column
        prop="className"
        label="班级名称">
      </el-table-column>
      <el-table-column
        prop="questionName"
        label="试卷名称">
      </el-table-column>
      <el-table-column
        prop="time"
        label="截止日期">
        <template slot-scope="scope">
          {{$Time(scope.row.time).getTime()}}
        </template>
      </el-table-column>
      <el-table-column
        prop="fraction"
        label="总分数">
      </el-table-column>
      <el-table-column
        label="我的分数">
        <template slot-scope="scope" v-if="testInfoList.filter(it => it.testId === scope.row.ids)[0]">
          {{testInfoList.filter(it => it.testId === scope.row.ids)[0].fraction}}
        </template>
      </el-table-column>
      <el-table-column
        width="160"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text"
                     :disabled="!!testInfoList.filter(it => it.testId === scope.row.ids).length"
                     @click="$routerGo('/admin-test-answer',scope.row)">答题</el-button>
        </template>
      </el-table-column>
    </dyd-table>
  </div>
</template>

<script>
  export default {
    name: "index",
    props: [''],
    data() {
      return {
        updates:false,
        form:{
          questionId: null,
          classId: null,
          userId:this.$adminInfo().ids,
          time:Date.now()
        },
        ruleForm:{},
        rules:{
          questionId:[
            {  required: true, message: '请选择试卷', trigger: 'blur'  },
          ],
          classId:[
            {  required: true, message: '请选择班级', trigger: 'blur'  },
          ],
          time:[
            {  required: true, message: '请选择考试时间', trigger: 'blur'  },
          ],
          name:[
            {  required: true, message: '请输入考试标题', trigger: 'blur'  },
          ]
        },
        tableData:[],
        classList:[],
        questionList:[],
        testInfoList:[]
      }
    },
    methods: {
      async getTest(){
        let cnt ={
          $orderBY: {col:'ids'},
          $where:{classId:this.$adminInfo().classId},
          $link:[
            {
              table:'classes',
              left:'classId',
              right:'classId1',
              where:'ids as classId1,name as className'
            },{
              table:'question',
              left:'questionId',
              right:'questionId',
              where:'ids as questionId,name as questionName,fraction'
            },
            // {
            //   table:'test_info',
            //   left:'userId',
            //   right:'userId',
            //   where:'ids as questionId,name as questionName,fraction'
            // },
          ]
        }
        if(!this.$adminInfo().ids) delete cnt.$where
        this.$api.get('test',cnt,res=>{
          this.tableData.push(...res)
        })
      },
      async getTestInfo(){
        let cnt = {
          $where:{
            userId:this.$adminInfo().ids
          }
        }
        this.$api.get('test_info',cnt,res=>{
          this.testInfoList = res
        })
      }

    },
    mounted() {
      this.getTest()
      this.getTestInfo()
    },
    created() {

    },
    components: {},
    watch: {},
  }
</script>

<style scoped lang='scss'>
  #index {

  }
</style>
